package com.cloud.security.controller;

import com.cloud.common.constants.GlobalConstants;
import com.cloud.common.domain.Result;
import com.cloud.controller.BaseController;
import com.cloud.domain.LoginUser;
import com.cloud.security.aop.RequestPermissions;
import com.cloud.security.entity.OnlineUser;
import com.cloud.security.service.IOnlineUserService;
import com.cloud.service.RedisService;
import com.cloud.utils.StringUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

@Tag(name = "在线用户监控")
@RestController
@RequestMapping("/security/online")
public class OnlineUserController extends BaseController {

    @Autowired
    private IOnlineUserService userOnlineService;

    @Autowired
    private RedisService redisService;

    @Operation(summary = "查询在线用户列表")
    @Parameters({
            @Parameter(name = "ipAddr", description = "请求IP地址"),
            @Parameter(name = "userName", description = "账户名称")
    })
    @RequestPermissions("security:online:list")
    @GetMapping("/list")
    public Result list(String ipAddr, String userName) {
        Collection<String> keys = redisService.keys(GlobalConstants.sysLoginTokens + "*");
        List<OnlineUser> userOnlineList = new ArrayList<>();
        for (String key : keys) {
            LoginUser user = redisService.get(key);
            if (StringUtils.isNotEmpty(ipAddr) && StringUtils.isNotEmpty(userName)) {
                if (StringUtils.equals(ipAddr, user.getIpAddr())
                        && StringUtils.equals(userName, user.getUser().getUserName())) {
                    userOnlineList.add(userOnlineService.selectOnlineByInfo(ipAddr, userName, user));
                }
            } else if (StringUtils.isNotEmpty(ipAddr)) {
                if (StringUtils.equals(ipAddr, user.getIpAddr())) {
                    userOnlineList.add(userOnlineService.selectOnlineByIpaddr(ipAddr, user));
                }
            } else if (StringUtils.isNotEmpty(userName) && StringUtils.isNotNull(user.getUser())) {
                if (StringUtils.equals(userName, user.getUser().getUserName())) {
                    userOnlineList.add(userOnlineService.selectOnlineByUserName(userName, user));
                }
            } else {
                userOnlineList.add(userOnlineService.loginUserToUserOnline(user));
            }
        }
        Collections.reverse(userOnlineList);
        userOnlineList.removeAll(Collections.singleton(null));
        return getDataTable(userOnlineList);
    }

    @Operation(summary = "强退用户账户")
    @Parameter(name = "tokenId", description = "账户Token")
    @RequestPermissions("security:online:remove")
    @PostMapping("/remove")
    public Result remove(String tokenId) {
        // 移除用户登录及关联系统信息
        redisService.del(GlobalConstants.sysLoginTokens + tokenId);
        redisService.del(GlobalConstants.sysUserProject + tokenId);
        return Result.success("操作成功");
    }

}
